package com.jxl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jxl.entity.po.Schedule;
import com.jxl.entity.po.Vehicle;
import com.jxl.entity.query.VehiclePageQuery;
import com.jxl.entity.vo.PageResponse;
import com.jxl.exception.BaseException;
import com.jxl.mapper.VehicleMapper;
import com.jxl.service.IScheduleService;
import com.jxl.service.IVehicleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 车辆表 服务实现类
 * </p>
 *
 * @author 金小梁
 * @since 2025-06-08
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements IVehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    private IScheduleService scheduleService;
    /**
     * 分页查询车辆信息
     *
     * @param vehiclePageQuery
     * @return
     */
    @Override
    public PageResponse<Vehicle> queryVehicles(VehiclePageQuery vehiclePageQuery) {
        PageHelper.startPage(vehiclePageQuery.getPageNum(), vehiclePageQuery.getPageSize());
        Page<Vehicle> vehiclePage = vehicleMapper.selectByPageQuery(vehiclePageQuery);
        PageResponse<Vehicle> pageResponse = PageResponse.of(vehiclePage, Vehicle.class);
        return pageResponse;
    }

    /**
     * 批量删除车辆
     *
     * @param vehicleIds
     */
    @Override
    public void deleteByIds(List<Integer> vehicleIds) {
        //根据车辆id查询车辆的信息
        List<Vehicle> vehicleList = vehicleMapper.selectBatchIds(vehicleIds);
        //根据车牌号查询车辆是否被排班
        vehicleList.forEach(vehicle -> {
            List<Schedule> list = scheduleService.query().eq("plate_no", vehicle.getPlateNo()).list();
            if (list.size()>0) {
                throw new BaseException("该车辆正在被排班，请先取消排班");
            }
        });
        removeByIds(vehicleIds);
    }
}
